<?php
/**
 * Created by PhpStorm.
 * User: function
 * Date: 2018-04-29
 * Time: 23:30
 */

namespace app\api\controller\v1;
use app\common\controller\Api;
use app\common\model\Address as AddressModel;

class Address extends Api
{
    
    public function index(){
        
        $params = paramForm();
        if (isset($params['action'])){
            switch ($params['action']){
                case 'getDefault':
                    $default = AddressModel::get(['user_id'=>$this->getUserId(),'ismain'=>1]);
                    if(!$default){
                        $default = AddressModel::get(['user_id'=>$this->getUserId()]);
                    }
                    if(!$default){
                        error('请先添加收货地址');
                    }
                    success($default);
                    break;
                case 'add':
                    $this->save($params);
                    break;
                case 'delete':
                    $this->delete($params);
                    break;
                case 'read':
                    $this->read($params);
                    break;
                case 'update':
                    $this->update($params);
                    break;
                case 'setDefault':
                    $default = AddressModel::get(['user_id'=>$this->getUserId(),'ismain'=>1]);
                    if($default){
                        $default->ismain = 0;
                        $default->isUpdate()->save();
                    }
                   
                    $address = AddressModel::get($params['id']);
                    $address->ismain = 1;
                    $rt = $address->isUpdate()->save();
                    if($rt){
                        success(null);
                    }else{
                        error('修改失败');
                    }
                    break;
                default:
                    break;
            }
            die;
        }
       $list =  AddressModel::all(['user_id'=>$this->getUserId()]);
        success($list);
        
    }
    
    public function save($params){
        $rule = [
            'link'=>'require',
            'user_id'=>'require',
            'link'=>'require',
            'province'=>'require',
            'area'=>'require',
            'city'=>'require'
        ];
       
        $params['user_id'] = $this->getUserId();
        $this->validate($params,$rule);
    
        $default = AddressModel::get(['user_id'=>$this->getUserId(),'ismain'=>1]);
        if(!$default){
           $params['ismain'] =1;//第一个就是默认地址
        }
        
        $addr = new AddressModel();
        $rt = $addr->allowField(true)->save($params);
    
       
        
        if($rt){
            success($addr->id);
        }else{
            error('添加失败');
        }
        
    }
    
    public function update($params){
    
        $address = new AddressModel();
        $rt = $address->allowField(true)->save($params,['id'=>$params['id']]);
        
        if($rt){
            success(null);
        }else{
            error('操作失败');
        }
        
    }
    
    public function delete($params){
        $address= AddressModel::get(['user_id'=>$this->getUserId(),'id'=>$params['id']]);
        if($address){
            if($address->ismain==1){
                $one = AddressModel::get(['user_id'=>$this->getUserId()]);
                $one->ismain = 1;
                $one->isUpdate()->save();
            }
            $address->delete();
        }else{
            error('该地址不存在');
        }
        
        success('删除成功');
    
        
        
        
    
    }
    
}